package edu.ynmd.cms.action;


import com.mysql.fabric.xmlrpc.base.Params;
import edu.ynmd.cms.model.*;
import edu.ynmd.cms.service.ManageService;
import edu.ynmd.cms.tools.JwtUtil;
import edu.ynmd.cms.vo.Parms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("public")
public class PublicAction {

    @Autowired
    private ManageService manageService;

    @PostMapping("login")
    @ResponseBody
    public HashMap login(@RequestBody Users user) throws Exception{
       HashMap m=new HashMap();

        Users u=manageService.getUsersByUsernameAndPass(user.getUsername(),user.getPass());

        if(u!=null){
            m.put("msg","ok");
            String token= JwtUtil.generateToken(u.getRole(),u.getUserid());
            m.put("token",token);

        }else{
            m.put("msg","error");
        }
return m;

    }

    @GetMapping("getNewsList")
    @ResponseBody
    public List<News> getNewsList() throws Exception{

        return manageService.getNewsList();
    }

    @GetMapping("getCarouselList")
    @ResponseBody
    public  List<Carousel> getCarouselList() throws Exception{


        return manageService.getCarouselList();
    }

    @GetMapping("getCourseList")
    @ResponseBody
    public List<Course> getCourseList() throws Exception{
        return manageService.getCourseList();
    }

    @GetMapping("getBookList")
    @ResponseBody
    public List<Book> getBookList() throws Exception{
        return manageService.getBookList();
    }

    @GetMapping("getClassroomList")
    @ResponseBody
    public List<Classroom> getClassroomList()throws Exception {
        return manageService.getClassroomList();
    }

    @GetMapping("getStudentList")
    @ResponseBody
    public List<Student> getStudentList()throws Exception {
        return manageService.getStudentList();
    }

    @GetMapping("getTeacherList")
    @ResponseBody
    public List<Teacher> getTeacherList()throws Exception {
        return manageService.getTeacherList();
    }


    @PostMapping("getCourseListByParm")
    @ResponseBody
    public List<Course> getCourseListByParm(@RequestBody Parms parms) throws Exception{

        List<Course> cl =new ArrayList();

        switch (parms.getKey()){
            case "book":
                cl = manageService.getCourseListByBook(parms.getValue());
                break;
            case "classroom":
                cl=manageService.getCourseListByClassroom(parms.getValue());
                break;
            case "student":
                cl=manageService.getCourseListByStudent(parms.getValue());
                break;
            case "teacher":
                cl=manageService.getCourseListByTeacher(parms.getValue());
                break;

        }

        return  cl;

    }
}
